package dormouse.vfs.ui;

import java.awt.Dimension;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JToggleButton;

import org.apache.commons.vfs2.FileObject;

/**
 * This button is used to interact with files. The class can be used in two
 * ways. If the file is a recognizable image (png, jpg, gif) you can pass a
 * FileObject to it
 */
public class FileButton extends JToggleButton  {

	static Map<String, Icon> iconMap16 = new HashMap<String, Icon>();

	static Map<String, Icon> iconMap32 = new HashMap<String, Icon>();

	static {
		
			iconMap16.put("jpg", IconFactory.getInstance(
					"/etc/gnome/gnome-image-jpeg.png"));
			iconMap16.put("png", IconFactory.getInstance(
					"/etc/gnome/gnome-image-png.png"));
			iconMap16.put("gif", IconFactory.getInstance(
					"/etc/gnome/gnome-image-gif.png"));
			iconMap16.put("svg", IconFactory.getInstance(
					"/etc/gnome/gnome-image-svg.png"));
			iconMap16.put("bmp", IconFactory.getInstance(
					"/etc/gnome/gnome-image-bmp.png"));
			iconMap16.put("pnm", IconFactory.getInstance(
					"/etc/gnome/gnome-image-pnm.png"));
			iconMap16.put("psd", IconFactory.getInstance(
					"/etc/gnome/gnome-image-psd.png"));
			iconMap16.put("ra", IconFactory.getInstance(
					"/etc/gnome/gnome-audio-pn-realaudio.png"));
			iconMap16.put("ac3", IconFactory.getInstance(
					"/etc/gnome/gnome-audio-ac3.png"));
			iconMap16.put("ppt", IconFactory.getInstance(
					"/etc/gnome/gnome-application-vnd-ms-powerpoint.png"));
			iconMap16.put("doc", IconFactory.getInstance(
					"/etc/gnome/gnome-application-msword.png"));
			iconMap16.put("xls", IconFactory.getInstance(
					"/etc/gnome/gnome-application-vnd-ms-excel.png"));
			iconMap16.put("txt", IconFactory
					.getInstance("/etc/gnome/i-regular.png"));
			iconMap16.put("java", IconFactory.getInstance(
					"/etc/gnome/gnome-text-x-java.png"));
			iconMap16.put("sh", IconFactory.getInstance(
					"/etc/gnome/gnome-text-x-sh.png"));
			iconMap16.put("pl", IconFactory.getInstance(
					"/etc/gnome/gnome-text-x-perl.png"));

			iconMap16.put("css", IconFactory.getInstance(
					"/etc/gnome/gnome-text-css.png"));
			iconMap16.put("html", IconFactory.getInstance(
					"/etc/gnome/gnome-text-html.png"));
			iconMap16.put("pdf", IconFactory.getInstance(
					"/etc/gnome/gnome-text-pdf-48.png"));
			iconMap16.put("xml", IconFactory.getInstance(
					"/etc/gnome/gnome-text-xml.png"));
			iconMap16.put("mpg", IconFactory.getInstance(
					"/etc/gnome/gnome-video-mpeg.png"));
			iconMap16.put("mov", IconFactory
					.getInstance("/etc/gnome/gnome-video-quicktime.png"));
			iconMap16.put("rtf", IconFactory
					.getInstance("/etc/gnome/gnome-application-rtf.png"));
			iconMap16.put("jar", IconFactory.getInstance(
					"/etc/gnome/gnome-application-x-jar.png"));
			iconMap16.put("class", IconFactory.getInstance(
					"/etc/gnome/gnome-application-x-jar.png"));

			iconMap16.put("dir", IconFactory.getInstance(
					"/etc/gnome/gnome-fs-directory.png"));

			iconMap32.put("jpg", IconFactory
					.getInstance("/etc/gnome/gnome-image-jpeg_32.png"));
			iconMap32.put("png", IconFactory
					.getInstance("/etc/gnome/gnome-image-png_32.png"));
			iconMap32.put("gif", IconFactory
					.getInstance("/etc/gnome/gnome-image-gif_32.png"));
			iconMap32.put("svg", IconFactory
					.getInstance("/etc/gnome/gnome-image-svg_32.png"));
			iconMap32.put("bmp", IconFactory
					.getInstance("/etc/gnome/gnome-image-bmp_32.png"));
			iconMap32.put("pnm", IconFactory
					.getInstance("/etc/gnome/gnome-image-pnm_32.png"));
			iconMap32.put("psd", IconFactory
					.getInstance("/etc/gnome/gnome-image-psd_32.png"));
			iconMap32.put("ra", IconFactory
					.getInstance("/etc/gnome/gnome-audio-pn-realaudio_32.png"));
			iconMap32.put("ac3", IconFactory
					.getInstance("/etc/gnome/gnome-audio-ac3_32.png"));
			iconMap32
					.put(
							"ppt",
							IconFactory
									.getInstance("/etc/gnome/gnome-application-vnd-ms-powerpoint_32.png"));
			iconMap32.put("doc", IconFactory
					.getInstance("/etc/gnome/gnome-application-msword_32.png"));
			iconMap32
					.put(
							"xls",
							IconFactory
									.getInstance("/etc/gnome/gnome-application-vnd-ms-excel_32.png"));
			iconMap32.put("txt", IconFactory
					.getInstance("/etc/gnome/i-regular_32.png"));
			iconMap32.put("java", IconFactory
					.getInstance("/etc/gnome/gnome-text-x-java_32.png"));
			iconMap32.put("sh", IconFactory
					.getInstance("/etc/gnome/gnome-text-x-sh_32.png"));
			iconMap32.put("pl", IconFactory
					.getInstance("/etc/gnome/gnome-text-x-perl_32.png"));

			iconMap32.put("css", IconFactory
					.getInstance("/etc/gnome/gnome-text-css_32.png"));
			iconMap32.put("html", IconFactory
					.getInstance("/etc/gnome/gnome-text-html_32.png"));
			iconMap32.put("pdf", IconFactory
					.getInstance("/etc/gnome/gnome-text-pdf-48_32.png"));
			iconMap32.put("xml", IconFactory
					.getInstance("/etc/gnome/gnome-text-xml_32.png"));
			iconMap32.put("mpg", IconFactory
					.getInstance("/etc/gnome/gnome-video-mpeg_32.png"));
			iconMap32.put("mov", IconFactory
					.getInstance("/etc/gnome/gnome-video-quicktime_32.png"));
			iconMap32.put("rtf", IconFactory
					.getInstance("/etc/gnome/gnome-application-rtf_32.png"));
			iconMap32.put("jar", IconFactory
					.getInstance("/etc/gnome/gnome-application-x-jar_32.png"));
			iconMap32.put("class", IconFactory
					.getInstance("/etc/gnome/gnome-application-x-jar_32.png"));

			iconMap32.put("dir", IconFactory
					.getInstance("/etc/gnome/gnome-fs-directory_32.png"));

		

	}

	/**
	 * 
	 */
	private static final long serialVersionUID = -1792133419033563668L;

	private FilePopupMenu popupMenu;
	private FileObject file = null;

	/**
	 * Use this constructor for creating a FileButton using a file object.
	 * 
	 * @param file
	 * @param useStdIcon
	 * @param iconSizeConstant
	 */
	public FileButton(FileObject file, boolean useStdIcon, int iconSizeConstant) {
		super();
		ImageIcon icon = null;
		this.file = file;
		Map<String, Icon> iconMap = getIconMap(iconSizeConstant);
		// use extension to find standard icon
		if (useStdIcon) {
			icon = (ImageIcon)IconFactory.getInstance(file, iconSizeConstant);

		} else { // otherwise create a thumbnail
			try {
				icon = (ImageIcon) IconFactory.getInstance(file.getName()
						.getURI(), iconSizeConstant);
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
		Dimension dim = new Dimension(128, 128);
		
		this.setMaximumSize(dim);
		this.setPreferredSize(dim);

		if (icon == null) { // if we can't figure out what icon to use, create a
			// default doc icon
			icon = (ImageIcon) iconMap.get("txt");
		}

		this.setIcon(icon);
		this.setText(getShortName(file.getName().getBaseName()));
		this.setToolTipText(file.getName().getBaseName());
		this.setVerticalTextPosition(JButton.BOTTOM);
		this.setHorizontalTextPosition(JButton.CENTER);
		
		this.addItemListener(new ItemListener(){

			public void itemStateChanged(ItemEvent e) {
				int state = e.getStateChange();
				if (state == ItemEvent.SELECTED){
					//TODO fix this with a file selection event
					//SelectedFileList.getInstance().add(FileButton.this.file);
				}else {
					// TODO fix this with a file selection event
					//SelectedFileList.getInstance().remove(FileButton.this.file);
				}
				
			}
			
		});
		
		this.addMouseListener(new MouseListener(){
			
			public void mouseClicked(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showPopup(e);
				}

			}

			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showPopup(e);
				}

			}

			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showPopup(e);
				}
				

			}

			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			public void mouseExited(MouseEvent e) {
				FileButton.this.popupMenu = getPopupMenu();
				FileButton.this.popupMenu.setVisible(false);

			}
			
			
		});

	}
	
	private void showPopup(MouseEvent e){
		popupMenu = getPopupMenu();
		popupMenu.setLocation(e.getPoint());
		popupMenu.setVisible(true);
	}
	
	private String getShortName(String name){
		String shortName = name;
		if (name.length() > 10){
			shortName = name.substring(0,10) + "...";
		}
		return shortName;
	}

	private Map<String, Icon> getIconMap(int iconSizeConstant) {
		Map<String, Icon> iconMap = null;
		switch (iconSizeConstant) {
			case IconFactory.SIZE_16: {
				iconMap = iconMap16;
				break;
			}
			case IconFactory.SIZE_32: {
				iconMap = iconMap32;
				break;
			}
		}
		return iconMap;
	}

	/**
	 * An overload of the main constructor which uses a standard file icon.
	 * 
	 * @param file
	 */
	public FileButton(FileObject file) {
		this(file, true, IconFactory.SIZE_16);
	}


	private FilePopupMenu getPopupMenu() {
		if (popupMenu == null) {
			popupMenu = new FilePopupMenu();			
		}
		return popupMenu;
	}
}
